<?php
/**
 * 分页抽象类
 *
 * @author iwind <iwind.iwind@gmail.com>
 * @link http://www.4kiki.net/
 * @version $Id: IAbstractPager.php 313 2008-04-26 12:25:26Z iwind $
 * @package net.4kiki.pager
 */

/**
 * 分页抽象类
 * 
 * 实现了IPager接口的一些方法
 *
 * @version $Id: IAbstractPager.php 313 2008-04-26 12:25:26Z iwind $
 * @package net.4kiki.pager
 */
abstract class IAbstractPager extends IMagicObject implements IPager {
	private $keyword;
	private $contentSize;
	private $path;
	private $pageSize;
	private $properties;
	private $query;
	private $pageNumber;
	private $rows = array();
	
	/**
	 * 加载配置
	 * 
	 * 示例：
	 * <pre>
	 *		$pager = new IYahooPager();
	 *		$pager->load(array(
	 *			"contentSize" => TLog::manager("TLog")->count(),
	 *			"keyword" => "page",
	 *			"pageSize" => 10
	 *		));
	 *	</pre>
	 *
	 * @param array $config 配置
	 * @since 0.1
	 */
	public function load(array $config) {
		foreach ($config as $key=>$value) {
			IReflection::setProperty($this, $key, $value);
		}
	}
	
	public function getCurrentPageNo() {
		$in = in::getInstance();
		$keyword = $this->getKeyword();
		$pageNo = intval($in->$keyword);
		if ($pageNo <= 0) {
			$pageNo = 1;
		}
		return min($pageNo, $this->getPageNumber());
	}
	
	public function getStartOffset() {
		$offset = $this->pageSize * ($this->getCurrentPageNo() - 1);
		if ($offset < 0) {
			$offset = 0;
		}
		return $offset;
	}
	
	public function setContentSize($contentSize) {
		$this->contentSize =  intval($contentSize);
		if ($this->contentSize < 0) {
			throw new IException("content size '{$contentSize}' can't be small than 0");
		}
	}
	
	public function getContentSize() {
		return $this->contentSize;
	}
	
	public function setKeyword($keyword) {
		$this->keyword = $keyword;
	}
	/**
	 * 取得分页用的关键字
	 *
	 * 从0.1开始，如果没有关键字，则默认为page
	 * 
	 * @return string
	 */
	public function getKeyword() {
		if ($this->keyword == null) {
			$this->keyword = "page";
		}
		return $this->keyword;
	}
	
	public function setPageSize($pageSize) {
		$this->pageSize = intval($pageSize);
		if ($this->pageSize < 1) {
			throw new IException("page size '{$pageSize}' can't be small than 1");
		}
	}
	
	public function getPageSize() {
		return $this->pageSize;
	}
	
	public function setPath($path) {
		$this->path = $path;
	}
	
	public function getPath() {
		return $this->path;
	}
	
	public function setProperties(array $properties) {
		$this->properties = $properties;
	}
	
	public function getProperties() {
		return $this->properties;
	}
	
	public function setQuery($query) {
		if (is_array($query)) {
			$_query = array();
			foreach ($query as $key=>$value) {
				$_query [] = "{$key}=" . $value;
			}
			$query = implode("&", $_query);
		}
		$this->query = $query;
	}
	
	public function getQuery() {
		return $this->query;
	}
	
	public function getURL($pageNo) {
		$query = $this->getQuery();
		if (strstr($query, IPager::PAGER_VARIABLE_STRING)) {
			$query = str_replace(IPager::PAGER_VARIABLE_STRING, $pageNo, $query);
		}
		else {
			if ($query == "") {
				$query = $this->getKeyword() . "=" . $pageNo;
			}
			else {
				$query .= "&" . $this->getKeyword() . "=" . $pageNo;
			}
		}
		return $this->getPath() . "?" . $query;
	}
	
	public function getPageNumber() {
		return ceil($this->getContentSize()/$this->getPageSize());
	}
	
	public function addRow($row) {
		$this->rows[] = $row;
	}
	
	public function addRows(array $rows) {
		foreach ($rows as $row) {
			$this->rows[] = $row;
		}
	}
	
	public function getRows() {
		return $this->rows;
	}
	
	public function setRows(array $rows) {
		$this->rows = $rows;
	}
}


?>